import { Popconfirm as AntPopconfirm, Space as AntSpace } from "antd";

export enum cardTypeType {
  TIME = "time",
  DURATION = "duration",
}

export interface cardType {
  id: string;
  name: string;
  type: cardTypeType;
  time: number;
  validityDay: number;
  courseId: string;
  orgId: string;
}

export const cardColumnsConfig = (onDeleteHandler: (key: string) => void) => {
  return [
    {
      title: "序号",
      dataIndex: "id",
      width: 50,
      editable: false,
      align: "center",
      render: (_d: any, _r: any, index: number) => index + 1,
    },
    {
      title: "名称",
      dataIndex: "name",
      valueType: "text",
      width: 120,
      align: "center",
    },
    {
      title: "有效期(天)",
      dataIndex: "validityDay",
      valueType: "digit",
      width: 110,
      align: "center",
    },
    {
      title: "可使用次数",
      dataIndex: "time",
      valueType: "digit",
      width: 110,
      align: "center",
    },
    {
      title: "类型",
      dataIndex: "type",
      valueType: "select",
      width: 100,
      align: "center",
      request() {
        return [
          {
            value: cardTypeType.TIME,
            label: "次数卡",
          },
          {
            value: cardTypeType.DURATION,
            label: "时长卡",
          },
        ];
      },
    },
    {
      title: "操作",
      valueType: "option",
      width: 150,
      align: "center",
      render: (
        _text: string,
        record: { id: string },
        _: any,
        action: { startEditable: (arg: any) => void }
      ) => (
        <AntSpace>
          <a
            key="edit"
            onClick={() => {
              action?.startEditable(record.id || "");
            }}
          >
            编辑
          </a>
          <AntPopconfirm
            title="提醒"
            description="确认要删除吗？"
            onConfirm={() => onDeleteHandler(record.id)}
          >
            <a key="delete">删除</a>
          </AntPopconfirm>
        </AntSpace>
      ),
    },
  ];
};

export const getMaxKey = (cards: cardType[] | []) => {
  let maxKey = 1;
  if (cards.length > 0) {
    cards.forEach((item) => {
      maxKey = Math.max(maxKey, Number(item.id));
    });
  }
  return maxKey + 1;
};
